Skip to content

Tell CLI users where to get an API key when it is missing or rejected - #460

Open
officialasishkumar wants to merge 1 commit into
useagenthq:mainfrom
officialasishkumar:feature/cli-api-key-help
Open

officialasishkumar wants to merge 1 commit into
useagenthq:mainfrom
officialasishkumar:feature/cli-api-key-help

Conversation

@officialasishkumar

@officialasishkumar officialasishkumar commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

By opening this pull request you agree to the Contributor License Agreement. On your first pull request a bot asks you to sign it with a one-line comment.

Fixes #255

The CLI said only "USEAGENT_API_KEY is not set" or "POST /api/runs -> HTTP 401", with no hint that keys are made in Settings, API keys.

What it prints now

$ useagent run hi            # no key set
error: USEAGENT_API_KEY is not set. Create a key in Settings, API keys (https://app.useagent.org/settings#apikeys), then export USEAGENT_API_KEY=<key>

$ useagent run hi            # server answers 401
error: https://app.useagent.org did not accept USEAGENT_API_KEY (HTTP 401). If it was revoked, mistyped or made on another server, create a key in Settings, API keys (https://app.useagent.org/settings#apikeys) and export it as USEAGENT_API_KEY

$ useagent status RUN_ID     # server answers 403
error: https://app.useagent.org accepted USEAGENT_API_KEY but refused the request (HTTP 403). Check that the key's account is still a member of the workspace it was made in

All three exit 1. The link and the server name come from USEAGENT_BASE_URL, so a self-hosted server points at its own Settings page. The 401 line names the server, which makes a key from another server easy to spot. The 403 line never calls the key invalid.

What changed

packages/cli

  • errors.ts:
    • apiKeysUrl(baseUrl) builds <base url>/settings#apikeys.
    • apiKeyFailure(status, baseUrl) writes the 401 and 403 messages, and returns null for any other status.
    • toCliError(error, baseUrl) turns any failure into the CliError the bin prints.
  • config.ts: the missing-key error links to Settings, API keys. resolveBaseUrl is split out so the bin can name the server when a request fails.
  • bin.ts: the single catch handler goes through toCliError, so run, status and fan all print one line and exit with the right code.
  • commands.ts: fan dispatches never throw, so a rejected key used to come back as one dispatch_error line per task. When every dispatch was refused over the key, fan now fails once with the message. A batch where some tasks went through keeps its results as before.
  • mcp.ts: a tool that fails with a 401 or 403 returns the same message as its tool error, so a local agent sees the next step too.

packages/agent-client

  • A failed dispatchMany outcome now carries the HTTP status when the server answered. That lets fan tell a refused key from a bad task without matching message text. A network failure has no status.

Docs: the CLI README and docs-site/docs/getting-started/cli.mdx say where keys are made and what the 401 and 403 messages mean.

Tests

  • config.test.ts: the missing-key message, with the default and a custom base URL.
  • errors.test.ts (new):
    • the Settings link, with and without a trailing slash
    • the 401 and 403 messages, and null for other statuses
    • toCliError passing a usage error through with exit code 2
  • commands.test.ts:
    • run against a fake client that answers 401 fails with the rejected-key message and exit 1, printing nothing to stdout
    • status with a 403 gets the refused-access message, not the new-key one
    • fan fails once when every dispatch got a 401, and keeps the results of a batch where only some did
  • mcp.test.ts: toolFailure gives the key message on a 401 and keeps any other failure's own message.
  • agent-client/test/fleet.test.ts: a failed dispatch carries its status, and a network failure has none.

Checks run locally with Bun 1.3.14:

  • cd packages/cli && bun run test: 63 pass, 0 fail
  • cd packages/agent-client && bun test test/fleet.test.ts: 19 pass, 0 fail
  • tsc --noEmit (TypeScript 5.9.3) in packages/cli and packages/agent-client: passes
  • bun test scripts/agent-docs.test.ts: 5 pass

I also ran bun packages/cli/src/bin.ts against a local stub server that answers 401 or 403. run, status and fan printed the messages above and exited 1, and with no key set it printed the missing-key line.


Prove it works
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

The CLI said only "USEAGENT_API_KEY is not set" or "POST /api/runs -> HTTP
401", with no hint that keys are made in the web app.

- A missing key now names Settings, API keys with a link on the server the
  CLI talks to (<base url>/settings#apikeys) and says to export the key.
- A 401 says the server did not accept the key, names the server (so a key
  made on another server is easy to spot) and gives the same link. A 403 says
  the key was accepted but the request was refused, and points at the key's
  workspace instead of calling the key invalid.
- The bin's single failure funnel maps errors through toCliError, so run,
  status and fan all print one line and exit 1. fan, whose dispatches never
  throw, fails once with the message when every dispatch was refused, rather
  than writing one dispatch_error line per task; a batch where some tasks
  went through keeps its results.
- The MCP server returns the same message as the tool error, so a local
  agent sees the next step too.

To let fan tell a refused key from a bad task without reading the message,
a failed dispatchMany outcome now carries the HTTP status when the server
answered.

The CLI README and the CLI docs page describe the messages.

Closes useagenthq#255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tell CLI users where to get an API key when it is missing or rejected

1 participant